﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using RapChieuPhim.DTO;
using System.Collections;
using System.Data;

namespace RapChieuPhim.DAO
{
    class ChucVuDAO
    {
        public ChucVuDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaCV", id);
            DataTable dt = cn.SqlExcuteQuery("LayChucVuTheoID", param);
            ChucVuDTO ChucVu = new ChucVuDTO();

            if (dt.Rows.Count != 0)
            {
                ChucVu.MaCV = int.Parse(dt.Rows[0]["MaCV"].ToString());
                ChucVu.TenCV = dt.Rows[0]["TenCV"].ToString();
            }
            return ChucVu;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllChucVu");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ChucVuDTO ChucVu = new ChucVuDTO();
                    ChucVu.MaCV = int.Parse(dr["MaCV"].ToString());
                    ChucVu.TenCV = dr["TenCV"].ToString();
                    array.Add(ChucVu);
                }
            }
            return array;
        }

        public void Insert(ChucVuDTO ChucVu)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@TenCV", ChucVu.TenCV);
            cn.SqlExcuteNonQuery("InsertChucVu", param);
        }

        public void Update(ChucVuDTO ChucVu)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaCV", ChucVu.MaCV);
            param[1] = new SqlParameter("@TenCV", ChucVu.TenCV);
            cn.SqlExcuteNonQuery("UpdateChucVu", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaCV", id);
            cn.SqlExcuteNonQuery("DeleteChucVu", param);
        }
    }
}
